package com.healthcloud.aop;

import com.alibaba.fastjson.JSON;
import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.BusInstrumentEntity;
import com.healthcloud.entity.BusRentEntity;
import com.healthcloud.entity.SysLogEntity;
import com.healthcloud.entity.SysUserEntity;
import com.healthcloud.service.BusInstrumentService;
import com.healthcloud.service.BusRentService;
import com.healthcloud.service.SysLogService;
import com.healthcloud.utils.HttpContextUtils;
import com.healthcloud.utils.IPUtils;
import com.healthcloud.utils.ShiroUtils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Objects;

/**
 * 系统日志，切面处理类
 *
 * Created by yu on 2017/6/6.
 */
@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;
    
    @Autowired
    private BusInstrumentService instrService;
    
    @Autowired
    private BusRentService busRentService;

    @Pointcut("@annotation(com.healthcloud.annotation.SysLog)")
    public void logPointCut() {

    }


    @Before("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLogEntity sysLog = new SysLogEntity();
        SysLog syslog = method.getAnnotation(SysLog.class);
        if(syslog != null){
            //注解上的描述
            sysLog.setOperation(syslog.value());
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        //请求的参数
        Object[] args = joinPoint.getArgs();
        String params = JSON.toJSONString(args[0]);
        sysLog.setParams(params);

        //删除修改保存原来数据
        String event = "";
//        if (args[0] instanceof BusHospitalEntity) {
//        	BusHospitalEntity hospital = (BusHospitalEntity) args[0];
//        	BusHospitalEntity oldHospital = hospService.searchById(hospital.getHospId());
//        	if (oldHospital!=null) {
//        		event += JSON.toJSONString(oldHospital);
//			}
//		}else
        if (args[0] instanceof BusInstrumentEntity) {
			BusInstrumentEntity instrument = (BusInstrumentEntity) args[0];
			BusInstrumentEntity oldInstrument = instrService.searchById(instrument.getInstrumentId());
        	if (oldInstrument!=null) {
        		event += JSON.toJSONString(oldInstrument);
			}
	    }else if (args[0] instanceof BusRentEntity) {
	    	BusRentEntity rent = (BusRentEntity) args[0];
	    	BusRentEntity oldRent = busRentService.searchById(rent.getRentId());
	    	if (oldRent!=null) {
	    		event += JSON.toJSONString(oldRent);
	    	}
	    }
        
        event += "-->";
        event += params;
        sysLog.setEvent(event);
        
        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        if (Objects.nonNull(userEntity)) {
            //用户名
            String username = userEntity.getUsername();
            sysLog.setUsername(username);
        }


        sysLog.setCreateDate(new Date());
        //保存系统日志
        sysLogService.save(sysLog);
    }
}
